<?php
namespace Admin\Controller;
use Think\Controller;
class databaseController extends AuthController {

   public function _initialize(){
         
		 parent::_initialize();
    }

    public function index(){
	    $databse = D()->db();
        $db      = $databse->getTables();

	    $lista  = $databse->query('SHOW TABLE STATUS');
	    $lista  = array_map('array_change_key_case', $lista);
	    //echo "<pre>";
		//print_r($lista);
	    $this->assign(array(
		    'db'  => $db,
			'list'=> $lista,
		)); 
        $this->display();
    }
	
    public function backup(){
	   $dbname  = I("post.dbname");
	   if(empty($dbname)){
	      $this->error("请选择至少一个表");
	   }else{
	     $table=$dbname;
	   }
	   $struct = $this->bakStruct($table);
	   $record = $this->bakRecord($table);
	   $sqls   = $struct.$record;
	   $dir    = "./Public/backup/backup_".date("Ymdhis").".sql";
	   file_put_contents($dir,$sqls);
	   if(file_exists($dir)){
	      $this->success("备份成功");
	   }else{
	      $this->error("备份失败");
	   }
    }
	
	/*备份表结构*/
	protected function bakStruct($array){
	    foreach ($array as $m => $v){
	      $tbName=$v;
	      $result=M()->query('show columns from '.$tbName);
	
	      $sql.="--\r\n";
	      $sql.="-- 数据表结构: `$tbName`\r\n";
	      $sql.="--\r\n\r\n";
	      //$sql.="DROP TABLE IF EXISTS `$tbName`;\r\n";
	      $sql.="create table `$tbName` (\r\n";
	      $rsCount      = count($result);
	      foreach ($result as $k=>$r){
	          $field    =       $r['field'];
	          $type     =       $r['type'];
	          $default  =       $r['default'];
	          $extra    =       $r['extra'];
	          $null     =       $r['null'];
			  $key      =       $r['key'];
			  //echo "<pre>";
		     // print_r($r);
			  if(!empty($default)){
			     $default='default \''.$default.'\'';
			  }else{
			      $default='';
			  }
	          if($null=='NO'){
	             $null  = 'NOT NULL';
	          }else{
	             $null  = "";
	          }          
			  if(trim($key) == 'PRI'){
			      $keys  = 'PRIMARY KEY  (`'. $field .'`) ';
			  }else{
			      $keys  = '';
			  }
			  
			 if($k<($rsCount-1)){
			     $sql.="`$field` $type $null $default $extra,\r\n";
				 if(!empty($keys)){
				     $sql = $sql.$keys.",\n";
				 }
				 
			 }else{
			     //最后一条不需要","号
				 if(!empty($keys)){
				     $sql = $sql.$keys.",\n";
				 }
			     $sql.="`$field` $type $null $default $extra\r\n";
				 
			 }
			 
	     }
	     $sql.=") ENGINE=MyISAM DEFAULT CHARSET=utf8;\r\n\r\n";
	 }
	 return str_replace(',)',')',$sql);
	}
	
	
	/*备份内容*/
	protected function bakRecord($array){
 
		foreach ($array as $v){
		     $tbName=$v;
             $rs=M()->query('select * from '.$tbName);
		 
		     if(count($rs)<=0){
			     continue;
		     }
		
		     $sql.="--\r\n";
		     $sql.="-- 数据表中的数据: `$tbName`\r\n";
		     $sql.="--\r\n\r\n";
		
		     foreach ($rs as $k=>$v){
		
		         $sql.="INSERT INTO `$tbName` VALUES (";
		         foreach ($v as $key=>$value){
		             if($value==''){
		                   $value='';
		             }
		             $type=gettype($value);
		             if($type=='string'){
		                  $value="'".addslashes($value)."'";
		             }
		             $sql.="$value," ;
		         }
		         $sql.=");\r\n\r\n";
		   }
	   }
	   return str_replace(',)',')',$sql);
   }
    
   /*数据库优化*/
   public function optimize(){
       $db_name = I("get.db_name");
	   $rs =M()->Query("OPTIMIZE TABLE `$db_name` ");
	   if($rs){
			$this->success("执行优化表".$db_name."成功");
		}else{
			$this->error("执行优化表".$db_name."失败".M()->GetError());
		}
   }
   
   /*数据库修复*/
   public function repair(){
       $db_name = I("get.db_name");
	   $rs      = M()->Query("REPAIR TABLE `$db_name` ");
	   if($rs){
			$this->success("执行修复表".$db_name."成功");
		}else{
			$this->error("执行修复表".$db_name."失败".M()->GetError());
		}
   }
   
   /*查看数据库结构*/
   public function showcreate(){
   
       $db_name = I("get.db_name");
	   $M = D();
	   $db->setAttribute(PDO::ATTR_CASE, PDO::CASE_UPPER); //设置属性
	   $dsql   = $db ->Query('SHOW CREATE TABLE `'. $db_name.'`' );
	   echo "<pre>";
	   print_r($M);
	   foreach($dsql as $k=>$v){
			foreach($v as $k1=>$v1){
				$rs=$v1;
		    }
	  }
      $this -> ajaxReturn($dsql);
   }

}